$(function() {
    toastr.options = messageOpts;

    $("#customerTable").bootstrapTable('destroy').bootstrapTable({
        url : window.portAddress + "/customer/customerList",
        dataType : "json",
        method : 'get',
        striped: true,  //行间隔色
        cache : false,  //禁用缓存
        pageNumber: 1,  //初始化加载第一页
        pagination: true,//是否分页
        sidePagination: "client",//前端分页
        pageSize: 5,//单页记录数
        pageList: [5, 10, 15],//可选择单页记录数
        clickToSelect : true,
        theadClasses: "thead-pink",
        queryParams: function (params) {
            //向服务器传递的参数
            return {
                customerNo : $("#customerNo").val(),
                customerGroup : $("#searchCustomerGroup").val(),
            };
        },
        responseHandler : function(res) {
            return res.data;
        },
        columns : [
            {
                title : "客户号",
                field : "customerNo",
                sortable : true,
                align : "center",
            },
            {
                title : "客户名称",
                field : "customerName",
                align : "center",
            },
            {
                title : "客户分组",
                field : "customerGroup",
                align : "center",
            },
            {
                title : "操作",
                field : "pendingNumber",
                formatter : operation,
                width : 130,
                height: 40,
                align : "center",
            }

        ]
    });

    $("#search").click(function() {
        $.ajax({
            url : window.portAddress + "/customer/customerList",
            type : "post",
            data : {
                customerNo : $("#customerNo").val(),
                customerGroup : $("#searchCustomerGroup").val(),
            },
            dataType : "json",
            success : function(data) {
                if (data.state === '000000') {
                    toastr.success("查询数据成功");
                }else {
                    toastr.error(data.message);
                }
                $("#customerTable").bootstrapTable("refresh");
            }
        })
    });

    function operation(value,row,index) {
        var htm;
        if(value != null){
            return  htm = "<span class=\"badge badge-warning\"><font size='3'>审核中...</font></span>"
        }else{
                htm = "<button type=\"button\" class=\"btn btn-primary\" style='width: 40px;' data-target=\"#updateModel\" data-toggle=\"modal\"><span class=\"glyphicon glyphicon-pencil\"></span></button> "
                    + "<button type=\"button\" class=\"btn btn-danger\" style='width: 40px;' data-target=\"#deleteModel\" data-toggle=\"modal\"><span class=\"glyphicon glyphicon-trash\"></span></button> ";
                return htm;
        }
    }
    var customerGroup;
    $("#updateModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var customerNumber = thisBtn.closest('tr').find('td').eq(0).text();
        var customerName = thisBtn.closest('tr').find('td').eq(1).text();
         customerGroup = thisBtn.closest('tr').find('td').eq(2).text();
        $("#customerNumber").val(customerNumber);
        $("#customerName").val(customerName);
        $("#customerGroup").val(customerGroup);
    });

    $("#deleteModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var customerNumber = thisBtn.closest('tr').find('td').eq(0).text();
        $("#deleteCustomerNumber").val(customerNumber);
    });

    $("#deleteBtn").click(function() {
        $.ajax({
            url : window.portAddress + "/customer/deleteCustomer",
            type : "post",
            data : {
                customerNo : $("#deleteCustomerNumber").val(),
                userName: window.sessionStorage.getItem("userName")
            },
            dataType : "json",
            success : function(data) {
                $("#customerTable").bootstrapTable("refresh");
                $("#deleteModel").modal('hide');

                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });

    $("#updateBtn").click(function() {
        if($("#customerGroup").val() == customerGroup){
            return toastr.warning("请修改数据后再提交！");
        }
        $.ajax({
            url : window.portAddress + "/customer/updateCustomer",
            type : "post",
            data : {
                customerNo : $("#customerNumber").val(),
                customerGroup : $("#customerGroup").val(),
                userName: window.sessionStorage.getItem("userName")
            },
            dataType : "json",
            success : function(data) {
                $("#customerTable").bootstrapTable("refresh");
                $("#updateModel").modal('hide');
                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });

    $("#addBtn").click(function () {
        $("#addModel").modal('show');
    });

    $("#addCustomer").click(function () {

        var options = [];
        $('input[name="options"]:checked').each(function() {
            options.push($(this).val());
        });

        if(options.length < 2){
            toastr.warning("请至少选择两种交易货币");
            return;
        }

        if($("#addCustomerName").val() == "" || $("#addCustomerName").val() == null){
            toastr.warning("请输入公司名称");
            return;
        }

        $.ajax({
            url : window.portAddress + "/customer/addCustomer",
            type : "post",
            data : {
                customerName : $("#addCustomerName").val(),
                customerGroup : $("#addCustomerGroup").val(),
                userName: window.sessionStorage.getItem("userName"),
                optionCurrency: options.toString()
            },
            traditional: true,
            dataType : "json",
            success : function(data) {
                $("#customerTable").bootstrapTable("refresh");
                $("#addModel").modal('hide');

                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });
})
